﻿using Smile.Compatibility;
using System;
using System.IO;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;

namespace Smile.Xaml.Converter
{
    /// <summary>Bytes to image converter.</summary>
    public class BytesToImageConverter : IValueConverter
    {
        // @Public
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            try
            {
                var bytes = (byte[])value;
                var ms = new MemoryRandomAccessStream(bytes);
                var image = new BitmapImage();
                image.CreateOptions = BitmapCreateOptions.None;
                image.SetSourceAsync(ms).AsTask();
                return image;

            }
            catch
            {
                return new BitmapImage();
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
